package com.hwhp.web.console.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.dispatcher.mapper.ActionMapping;

import com.hwhp.common.BaseAction;
import com.hwhp.common.exception.yzdbException;
import com.hwhp.entity.Menu;
import com.hwhp.service.console.MenuService;
import com.hwhp.service.console.impl.MenuServiceImpl;
import com.hwhp.util.DateUtils;



/**
 * @author zyc
 *Jun 25, 2009
 * desc: curd action
 */
public class AdminMenus extends BaseAction{
	private MenuService menuService = null ;
	/**
	 * 菜单管理主页面初始化
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String initMenuProcess()throws yzdbException {
		String returnVlue="";
		//return mapping.findForward("adminmenu.init.success");
		return "adminmenu.init.success";
	}
	/**
	 * 初始化菜单编辑界靄1�7
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String initMenuEidt()throws yzdbException {
		HttpServletRequest request = this.getHttpRequest();
		String returnVlue=request.getParameter("menuid");
		String processType=request.getParameter("processType");
		if(!"delete".equals(processType)){
			if(returnVlue!=null&&!"".equals(returnVlue)){
				Menu menu=menuService.getMenuById(returnVlue);
				request.setAttribute("menu", menu);
			}
		}
		
		//return mapping.findForward("adminmenu.edit.success");
		return "adminmenu.edit.success";
	}
	/**
	 * 初始化菜单�1�7�择栄1�7
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String initMenuTree()throws yzdbException {
		
		HttpServletRequest request = this.getHttpRequest();
		
		String rootid=request.getParameter("rootid");
		
		List<Menu> list = menuService.getAllSubMenuById(rootid, "1");
		
		//MenuService.getMenuById("111");
		System.out.println("initMenuTree list size:"+list.size());
		request.setAttribute("mentList", list);
		
		//return mapping.findForward("adminmenu.tree.success");
		return "adminmenu.tree.success" ;
	}
	/**
	 * 菜单处理，新增�1�7�修改�1�7�删附1�7
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String processMenu()throws yzdbException {
		HttpServletRequest request = this.getHttpRequest();
		String processType=request.getParameter("processType");
		if(processType!=null&&!"".equals(processType)){
			String menuId=request.getParameter("menuid");
			String menuName=request.getParameter("menuname");
			String menuCode=request.getParameter("code");
			String menuUrl=request.getParameter("menuurl");
			String menuImageUrl=request.getParameter("menuimageurl");
			String menuRemark=request.getParameter("remark");
			String menuParentId=request.getParameter("parentid");
			String menuParentName=request.getParameter("parentname");
			String menuParentCode=request.getParameter("parentcode");
			int menuGrade=0;
			int isUsed=1;
			String isused=request.getParameter("isused");
			if(isused!=null&&!"".equals(isused)){
				isUsed=Integer.parseInt(isused);
			}
			String menugrade=request.getParameter("menugrade");
			if(menugrade!=null&&!"".equals(menugrade)){
				menuGrade=Integer.parseInt(menugrade);
			}
			Date currentDate=DateUtils.getCurrDate();
			Menu menu=new Menu();
			//
			if(processType.equals("add")){
				menu.setName(menuName);
				menu.setCode(menuParentCode+menuCode);
				menu.setUrl(menuUrl);
				menu.setImageUrl(menuImageUrl);
				menu.setIsused(isUsed);
				menu.setRemark(menuRemark);
				menu.setCreatdate(currentDate);
				menu.setParentid(menuParentId);
				menu.setParentname(menuParentName);
				menu.setGrade(menuGrade+1);
				menuService.saveOrUpdateMenu(menu);
			}
			//修改
            if(processType.equals("update")){
            	menu=menuService.getMenuById(menuId);
            	menu.setName(menuName);
				menu.setCode(menuCode);
				menu.setUrl(menuUrl);
				menu.setImageUrl(menuImageUrl);
				menu.setIsused(isUsed);
				menu.setRemark(menuRemark);
				menu.setCreatdate(currentDate);
				menuService.saveOrUpdateMenu(menu);
			}
            //删除
            if(processType.equals("delete")){
            	menuService.deleteMenuById(menuId);
            }
			
		}
		return this.initMenuEidt();
	}
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
	public MenuService getMenuService() {
		return menuService;
	}
}
